package com.example.crossborderecommerce.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.crossborderecommerce.entity.Role;
import com.example.crossborderecommerce.entity.Result;
import com.example.crossborderecommerce.service.IRoleService;
import com.example.crossborderecommerce.utils.DecodeToken;
import com.example.crossborderecommerce.utils.TokenUtil;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 角色模块
 *
 * @author Yolanda
 * @since 2024-05-09
 */
@RestController
@Transactional
@RequestMapping("/role")
public class RoleController {
    @Resource
    IRoleService roleService;

    /**
     * 添加角色
     * @param role
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Role role) {
        boolean res = roleService.save(role);
        if(res) {
            return Result.success("添加角色成功！");
        }
        return Result.error("添加角色失败！");
    }

    /**
     * 获得角色总数
     * @return
     */
    @GetMapping("/count")
    public Result count() {
        return Result.success(roleService.count());
    }

    /**
     * 查询所有角色
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        List<Role> roleList = roleService.list();
        if(roleList != null) {
            return Result.success("查询所有角色成功！", roleList);
        }
        return Result.error("查询所有角色失败！");
    }

    /**
     *删除某个角色
     * @param role
     * @return
     */
    @PostMapping("/delete")
    public Result delete(Role role) {
        boolean res = roleService.removeById(role);
        if(res) {
            return Result.success("删除角色成功！");
        }
        return Result.error("删除角色失败！");
    }

    /**
     * 删除一系列角色
     * @param ids
     * @return
     */
    @GetMapping("/deleteBatch")
    public Result deleteBatch(@RequestParam String ids) {
        List<String> roleIds = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        boolean res = roleService.removeByIds(roleIds);
        if(res) {
            return Result.success("删除角色成功！");
        }
        return Result.error("删除角色失败！");
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Role role) {
        boolean res = roleService.saveOrUpdate(role);
        if (res) {
            return Result.success("修改角色成功！");
        }
        return Result.error("修改角色失败！");
    }

    /**
     * 分页查询所有的角色
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(@RequestParam(defaultValue = "") String name,
                           @RequestParam(defaultValue = "") String role,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if(!role.isEmpty()) {
            queryWrapper.eq("role", role);
        }
        queryWrapper.like("name", name);
        Page<Role> rolePage = roleService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success("成功查询所有角色信息！",rolePage);
    }

    /**
     * 根据权限查询角色名
     * @param authority
     * @return
     */
    @GetMapping("findNameByAuthority")
    public Result findNameByAuthority(@RequestParam(defaultValue = "") String authority) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("authority", authority);
        Role role = roleService.getOne(queryWrapper);
        if(role == null) {
            return Result.error("查询角色失败！");
        }
        return Result.success("查询角色成功！", role.getName());
    }
}
